// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 

package it.softphone.commons.util.web.proxy;

import static it.softphone.commons.util.LogUtil.logger;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletResponse;

public class GetPost
{
	
    @SuppressWarnings("unused")
	private final String NEWLINE  = System.getProperty("line.separator");
    
    private int errorCode = 0;;
	private final boolean ignoreContentType;
	
    public GetPost( boolean ignoreContentType )
    {
    	this.ignoreContentType = ignoreContentType;
    	
    	logger.info( new StringBuilder()
    		.append('\n')
    		.append("========================")	.append('\n')
    		.append("HTTPPROXY 0.0.2")			.append('\n')
    		.append("========================")	.append('\n')
    		.toString()
    	);
    	
    }


	protected void log( String msg ) {
		logger.info( msg );
	}
	
	protected java.io.InputStream createInputStream( URLConnection httpurlconnection ) throws Exception {
		java.io.InputStream result = null;
		
		String encoding = httpurlconnection.getContentEncoding();
		
	    if( encoding!=null && encoding.indexOf("gzip") >= 0) 
	        result = new BufferedInputStream(httpurlconnection.getInputStream());
	        //result = new GZIPInputStream(httpurlconnection.getInputStream());
	    else
	        result = new BufferedInputStream(httpurlconnection.getInputStream());

		
		return result;
	}    
    	
	public void setErrorCode(int i)
    {
        errorCode = i;
    }

    public int getErrorCode()
    {
        return errorCode;
    }

    public String doAction(	String method, 
    						String host, 
    						Hashtable hashtable, 
    						Hashtable<String,String> headerMap, 
    						int timeout, 
    						String proxyHost, 
    						String proxyPort, 
    						String encoding, 
    						ServletInputStream servletinputstream, 
    						HttpServletResponse httpservletresponse) throws IOException
    {
        if(timeout > 0)
        {
            Properties properties = System.getProperties();
            properties.put("sun.net.client.defaultConnectTimeout", (new StringBuilder()).append("").append(timeout).toString());
            properties.put("sun.net.client.defaultReadTimeout", (new StringBuilder()).append("").append(timeout).toString());
            System.setProperties(properties);
        }
        if(proxyHost != null && proxyPort != null)
        {
            Properties properties1 = System.getProperties();
            properties1.put("proxySet", "true");
            properties1.put("proxyHost", proxyHost);
            properties1.put("proxyPort", proxyPort);
            System.setProperties(properties1);
        }
        if("GET".equals(method.toUpperCase()))
            if(host.toUpperCase().startsWith("HTTPS://"))
                return doGetSSL(host, headerMap, encoding, httpservletresponse);
            else
                return doGet(host, headerMap, encoding, httpservletresponse);
        if(host.toUpperCase().startsWith("HTTPS://"))
            return doPostSSL(host, hashtable, headerMap, encoding, servletinputstream, httpservletresponse);
        else
            return doPost(host, hashtable, headerMap, encoding, servletinputstream, httpservletresponse);
    }

    public String doDelete(String host, Hashtable hashtable, Hashtable<String,String> headerMap, int timeout, String s1, String s2, String s3, ServletInputStream servletinputstream, HttpServletResponse httpservletresponse) throws IOException
    {
        if(timeout > 0)
        {
            Properties properties = System.getProperties();
            properties.put("sun.net.client.defaultConnectTimeout", (new StringBuilder()).append("").append(timeout).toString());
            properties.put("sun.net.client.defaultReadTimeout", (new StringBuilder()).append("").append(timeout).toString());
            System.setProperties(properties);
        }
        if(s1 != null && s2 != null)
        {
            Properties properties1 = System.getProperties();
            properties1.put("proxySet", "true");
            properties1.put("proxyHost", s1);
            properties1.put("proxyPort", s2);
            System.setProperties(properties1);
        }
        if(host.toUpperCase().startsWith("HTTPS://"))
            return doDeleteSSL(host, headerMap, s3, httpservletresponse);
        else
            return doDelete(host, headerMap, s3, httpservletresponse);
    }

    public String doHead(String host, Hashtable hashtable, Hashtable<String,String> headerMap, int timeout, String s1, String s2, String s3, ServletInputStream servletinputstream, HttpServletResponse httpservletresponse) throws IOException
    {
        if(timeout > 0)
        {
            Properties properties = System.getProperties();
            properties.put("sun.net.client.defaultConnectTimeout", (new StringBuilder()).append("").append(timeout).toString());
            properties.put("sun.net.client.defaultReadTimeout", (new StringBuilder()).append("").append(timeout).toString());
            System.setProperties(properties);
        }
        if(s1 != null && s2 != null)
        {
            Properties properties1 = System.getProperties();
            properties1.put("proxySet", "true");
            properties1.put("proxyHost", s1);
            properties1.put("proxyPort", s2);
            System.setProperties(properties1);
        }
        if(host.toUpperCase().startsWith("HTTPS://"))
            return doHeadSSL(host, headerMap, s3, httpservletresponse);
        else
            return doHead(host, headerMap, s3, httpservletresponse);
    }

    public String doPut(String host, Hashtable hashtable, Hashtable<String,String> headerMap, int timeout, String s1, String s2, String s3, ServletInputStream servletinputstream, HttpServletResponse httpservletresponse) throws IOException
    {
        if(timeout > 0)
        {
            Properties properties = System.getProperties();
            properties.put("sun.net.client.defaultConnectTimeout", (new StringBuilder()).append("").append(timeout).toString());
            properties.put("sun.net.client.defaultReadTimeout", (new StringBuilder()).append("").append(timeout).toString());
            System.setProperties(properties);
        }
        if(s1 != null && s2 != null)
        {
            Properties properties1 = System.getProperties();
            properties1.put("proxySet", "true");
            properties1.put("proxyHost", s1);
            properties1.put("proxyPort", s2);
            System.setProperties(properties1);
        }
        if(host.toUpperCase().startsWith("HTTPS://"))
            return doPutSSL(host, hashtable, headerMap, s3, servletinputstream, httpservletresponse);
        else
            return doPut(host, hashtable, headerMap, s3, servletinputstream, httpservletresponse);
    }

    private String doPut(String host, Hashtable hashtable, Hashtable<String,String> headerMap, String s1, ServletInputStream servletinputstream, HttpServletResponse httpservletresponse) throws IOException
    {
        Object obj = null;
        HttpURLConnection httpurlconnection = null;
        Object obj1 = null;
        BufferedOutputStream bufferedoutputstream = null;
        String s2 = "";
        if(hashtable != null)
            s2 = getParamsFromHash(hashtable);
        try
        {
            URL url = new URL(host);
            httpurlconnection = (HttpURLConnection)url.openConnection();
            httpurlconnection.setRequestMethod("PUT");
            httpurlconnection.setDoOutput(true);
            httpurlconnection.setDoInput(true);
            httpurlconnection.setUseCaches(false);            
            if( !ignoreContentType ) httpurlconnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            if(headerMap != null)
            {
                String s3;
                for(Enumeration enumeration = headerMap.keys(); enumeration.hasMoreElements(); httpurlconnection.setRequestProperty(s3, (String)headerMap.get(s3)))
                    s3 = (String)enumeration.nextElement();

            }
            PrintWriter printwriter = new PrintWriter(httpurlconnection.getOutputStream());
            printwriter.print(s2);
            int j;
            while((j = servletinputstream.read()) != -1) 
                printwriter.write(j);
            printwriter.close();
        }
        catch(Exception exception)
        {
            getErrorCodeFromResponse(httpurlconnection);
            return getMessage(host, exception);
        }
        try
        {
            setErrorCode(httpurlconnection.getResponseCode());
            String s4 = httpurlconnection.getContentType();
            if(s4 != null && s1 != null)
                s4 = rewriteEncoding(s4, s1);
            if(s4 != null)
                httpservletresponse.setContentType(s4);
            if(headerMap != null)
                rewriteHeaders(httpurlconnection, httpservletresponse);
            try
            {
            	obj1 = createInputStream(httpurlconnection);
                bufferedoutputstream = new BufferedOutputStream(httpservletresponse.getOutputStream());
                int i;
                while((i = ((InputStream) (obj1)).read()) >= 0) 
                    bufferedoutputstream.write(i);
            }
            catch(Exception exception2)
            {
                return getMessage(host, exception2);
            }
        }
        catch(Exception exception1)
        {
            return getMessage(host, exception1);
        }
        if(obj1 != null)
            ((InputStream) (obj1)).close();
        if(bufferedoutputstream != null)
        {
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        return null;
    }

    private String doPutSSL(String host, Hashtable hashtable, Hashtable<String,String> headerMap, String s1, ServletInputStream servletinputstream, HttpServletResponse httpservletresponse) throws IOException
    {
        Object obj = null;
        HttpsURLConnection httpsurlconnection = null;
        Object obj1 = null;
        BufferedOutputStream bufferedoutputstream = null;
        String s2 = "";
        if(hashtable != null)
            s2 = getParamsFromHash(hashtable);
        try
        {
            URL url = new URL(host);
            httpsurlconnection = (HttpsURLConnection)url.openConnection();
            httpsurlconnection.setRequestMethod("PUT");
            httpsurlconnection.setDoOutput(true);
            httpsurlconnection.setDoInput(true);
            httpsurlconnection.setUseCaches(false);
            if( !ignoreContentType ) httpsurlconnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            if(headerMap != null)
            {
                String s3;
                for(Enumeration enumeration = headerMap.keys(); enumeration.hasMoreElements(); httpsurlconnection.setRequestProperty(s3, (String)headerMap.get(s3)))
                    s3 = (String)enumeration.nextElement();

            }
            PrintWriter printwriter = new PrintWriter(httpsurlconnection.getOutputStream());
            printwriter.print(s2);
            int j;
            while((j = servletinputstream.read()) != -1) 
                printwriter.write(j);
            printwriter.close();
        }
        catch(Exception exception)
        {
            getErrorCodeFromResponse(httpsurlconnection);
            return getMessage(host, exception);
        }
        try
        {
            if(httpsurlconnection instanceof HttpURLConnection)
            {
                HttpsURLConnection httpsurlconnection1 = httpsurlconnection;
                setErrorCode(httpsurlconnection1.getResponseCode());
            }
            String s4 = httpsurlconnection.getContentType();
            if(s4 != null && s1 != null)
                s4 = rewriteEncoding(s4, s1);
            if(s4 != null)
                httpservletresponse.setContentType(s4);
            if(headerMap != null)
                rewriteSSLHeaders(httpsurlconnection, httpservletresponse);
            try
            {
            	obj1 = createInputStream(httpsurlconnection);
                bufferedoutputstream = new BufferedOutputStream(httpservletresponse.getOutputStream());
                int i;
                while((i = ((InputStream) (obj1)).read()) >= 0) 
                    bufferedoutputstream.write(i);
            }
            catch(Exception exception2)
            {
                return getMessage(host, exception2);
            }
        }
        catch(Exception exception1)
        {
            return getMessage(host, exception1);
        }
        if(obj1 != null)
            ((InputStream) (obj1)).close();
        if(bufferedoutputstream != null)
        {
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        return null;
    }

    private String doPost(String host, Hashtable hashtable, Hashtable<String,String> headerMap, String s1, ServletInputStream servletinputstream, HttpServletResponse httpservletresponse) throws IOException
    {
        Object obj = null;
        URLConnection urlconnection = null;
        Object obj1 = null;
        BufferedOutputStream bufferedoutputstream = null;
        String s2 = "";
        if(hashtable != null)
            s2 = getParamsFromHash(hashtable);
        try
        {
            URL url = new URL(host);
            urlconnection = url.openConnection();
            urlconnection.setDoOutput(true);
            urlconnection.setDoInput(true);
            urlconnection.setUseCaches(false);
            if( !ignoreContentType ) urlconnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            if(headerMap != null)
            {
                String s3;
                for(Enumeration enumeration = headerMap.keys(); enumeration.hasMoreElements(); urlconnection.setRequestProperty(s3, (String)headerMap.get(s3)))
                    s3 = (String)enumeration.nextElement();

            }
            PrintWriter printwriter = new PrintWriter(urlconnection.getOutputStream());
            printwriter.print(s2);
            int j;
            while((j = servletinputstream.read()) != -1) 
                printwriter.write(j);
            printwriter.close();
        }
        catch(Exception exception)
        {
            getErrorCodeFromResponse(urlconnection);
            return getMessage(host, exception);
        }
        try
        {
            if(urlconnection instanceof HttpURLConnection)
            {
                HttpURLConnection httpurlconnection = (HttpURLConnection)urlconnection;
                setErrorCode(httpurlconnection.getResponseCode());
            }
            String contentType = urlconnection.getContentType();
            
            if(contentType != null && s1 != null) {
                contentType = rewriteEncoding(contentType, s1);
                
            }
            
            if(contentType != null) {
                httpservletresponse.setContentType(contentType);
            }
            
            if(headerMap != null)
                rewriteHeaders(urlconnection, httpservletresponse);
            try
            {
            	obj1 = createInputStream(urlconnection);
                bufferedoutputstream = new BufferedOutputStream(httpservletresponse.getOutputStream());
                int i;
                while((i = ((InputStream) (obj1)).read()) >= 0) 
                    bufferedoutputstream.write(i);
            }
            catch(Exception exception2)
            {
                return getMessage(host, exception2);
            }
        }
        catch(Exception exception1)
        {
            return getMessage(host, exception1);
        }
        if(obj1 != null)
            ((InputStream) (obj1)).close();
        if(bufferedoutputstream != null)
        {
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        return null;
    }

    private String doPostSSL(String host, Hashtable hashtable, Hashtable<String,String> headerMap, String encoding, ServletInputStream servletinputstream, HttpServletResponse httpservletresponse) throws IOException
    {
        Object obj = null;
        HttpsURLConnection httpsurlconnection = null;
        Object obj1 = null;
        BufferedOutputStream bufferedoutputstream = null;
        String s2 = "";
        if(hashtable != null)
            s2 = getParamsFromHash(hashtable);
        try
        {
            URL url = new URL(host);
            httpsurlconnection = (HttpsURLConnection)url.openConnection();
            httpsurlconnection.setDoOutput(true);
            httpsurlconnection.setDoInput(true);
            httpsurlconnection.setUseCaches(false);
            if( !ignoreContentType ) httpsurlconnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            if(headerMap != null)
            {
                String s3;
                for(Enumeration<String> enumeration = headerMap.keys(); enumeration.hasMoreElements(); httpsurlconnection.setRequestProperty(s3, headerMap.get(s3)))
                    s3 = enumeration.nextElement();

            }
            PrintWriter printwriter = new PrintWriter(httpsurlconnection.getOutputStream());
            printwriter.print(s2);
            int j;
            while((j = servletinputstream.read()) != -1) 
                printwriter.write(j);
            printwriter.close();
        }
        catch(Exception exception)
        {
            getErrorCodeFromResponse(httpsurlconnection);
            return getMessage(host, exception);
        }
        try
        {
            if(httpsurlconnection instanceof HttpURLConnection)
            {
                HttpsURLConnection httpsurlconnection1 = httpsurlconnection;
                setErrorCode(httpsurlconnection1.getResponseCode());
            }
            String s4 = httpsurlconnection.getContentType();
            if(s4 != null && encoding != null)
                s4 = rewriteEncoding(s4, encoding);
            if(s4 != null)
                httpservletresponse.setContentType(s4);
            if(headerMap != null)
                rewriteSSLHeaders(httpsurlconnection, httpservletresponse);
            try
            {
            	obj1 = createInputStream(httpsurlconnection);
                bufferedoutputstream = new BufferedOutputStream(httpservletresponse.getOutputStream());
                int i;
                while((i = ((InputStream) (obj1)).read()) >= 0) 
                    bufferedoutputstream.write(i);
            }
            catch(Exception exception2)
            {
                return getMessage(host, exception2);
            }
        }
        catch(Exception exception1)
        {
            return getMessage(host, exception1);
        }
        if(obj1 != null)
            ((InputStream) (obj1)).close();
        if(bufferedoutputstream != null)
        {
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        return null;
    }

    private String doGet(String host, Hashtable<String,String> headerMap, String encoding, HttpServletResponse httpservletresponse) throws IOException
    {
        Object obj = null;
        URLConnection urlconnection = null;
        Object obj1 = null;
        BufferedOutputStream bufferedoutputstream = null;
        String s2 = "";
        try
        {
            URL url = new URL(host);
            urlconnection = url.openConnection();
            urlconnection.setDoInput(true);
            urlconnection.setUseCaches(false);
            if(headerMap != null)
            {
                String s3;
                for(Enumeration<String> enumeration = headerMap.keys(); enumeration.hasMoreElements(); urlconnection.setRequestProperty(s3, headerMap.get(s3)))
                    s3 = enumeration.nextElement();

            }
        }
        catch(Exception exception)
        {
            getErrorCodeFromResponse(urlconnection);
            return getMessage(host, exception);
        }
        try
        {
            if(urlconnection instanceof HttpURLConnection)
            {
                HttpURLConnection httpurlconnection = (HttpURLConnection)urlconnection;
                setErrorCode(httpurlconnection.getResponseCode());
            }
            String contentType = urlconnection.getContentType();
            if(contentType != null && encoding != null) {
                contentType = rewriteEncoding(contentType, encoding);
                log( "DOGET rewriteEncoding " + contentType);
            }
            if(contentType != null) {
                httpservletresponse.setContentType(contentType);
            }
            else {
                contentType = "";
            }
            log( "DOGET response ContentType " + contentType); 
                        
            if(headerMap != null)
                rewriteHeaders(urlconnection, httpservletresponse);
            try
            {
            	obj1 = createInputStream(urlconnection);
                bufferedoutputstream = new BufferedOutputStream(httpservletresponse.getOutputStream());
                int i;
                while((i = ((InputStream) (obj1)).read()) >= 0) 
                    bufferedoutputstream.write(i);
            }
            catch(Exception exception2)
            {
                return getMessage(host, exception2);
            }
        }
        catch(Exception exception1)
        {
            return getMessage(host, exception1);
        }
        if(obj1 != null)
            ((InputStream) (obj1)).close();
        if(bufferedoutputstream != null)
        {
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        return null;
    }

    private String doGetSSL(String host, Hashtable<String,String> headerMap, String encoding, HttpServletResponse httpservletresponse) throws IOException
    {
        Object obj = null;
        HttpsURLConnection httpsurlconnection = null;
        Object obj1 = null;
        BufferedOutputStream bufferedoutputstream = null;
        String s2 = "";
        try
        {
            URL url = new URL(host);
            httpsurlconnection = (HttpsURLConnection)url.openConnection();
            httpsurlconnection.setDoInput(true);
            httpsurlconnection.setUseCaches(false);
            if(headerMap != null)
            {
                String s3;
                for(Enumeration<String> enumeration = headerMap.keys(); enumeration.hasMoreElements(); httpsurlconnection.setRequestProperty(s3, headerMap.get(s3)))
                    s3 = enumeration.nextElement();

            }
        }
        catch(Exception exception)
        {
            getErrorCodeFromResponse(httpsurlconnection);
            return getMessage(host, exception);
        }
        try
        {
            if(httpsurlconnection instanceof HttpURLConnection)
            {
                HttpsURLConnection httpsurlconnection1 = httpsurlconnection;
                setErrorCode(httpsurlconnection1.getResponseCode());
            }
            String s4 = httpsurlconnection.getContentType();
            if(s4 != null && encoding != null)
                s4 = rewriteEncoding(s4, encoding);
            if(s4 != null)
                httpservletresponse.setContentType(s4);
            else
                s4 = "";
            if(headerMap != null)
                rewriteSSLHeaders(httpsurlconnection, httpservletresponse);
            try
            {
            	obj1 = createInputStream(httpsurlconnection);
                bufferedoutputstream = new BufferedOutputStream(httpservletresponse.getOutputStream());
                int i;
                while((i = ((InputStream) (obj1)).read()) >= 0) 
                    bufferedoutputstream.write(i);
            }
            catch(Exception exception2)
            {
                return getMessage(host, exception2);
            }
        }
        catch(Exception exception1)
        {
            return getMessage(host, exception1);
        }
        if(obj1 != null)
            ((InputStream) (obj1)).close();
        if(bufferedoutputstream != null)
        {
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        return null;
    }


    private String doDelete(String host, Hashtable<String,String> headerMap, String encoding, HttpServletResponse httpservletresponse) throws IOException
    {
        Object obj = null;
        HttpURLConnection httpurlconnection = null;
        Object obj1 = null;
        BufferedOutputStream bufferedoutputstream = null;
        String s2 = "";
        try
        {
            URL url = new URL(host);
            httpurlconnection = (HttpURLConnection)url.openConnection();
            httpurlconnection.setRequestMethod("DELETE");
            httpurlconnection.setDoInput(true);
            httpurlconnection.setUseCaches(false);
            if(headerMap != null)
            {
                String s3;
                for(Enumeration<String> enumeration = headerMap.keys(); enumeration.hasMoreElements(); httpurlconnection.setRequestProperty(s3, headerMap.get(s3)))
                    s3 = (String)enumeration.nextElement();

            }
        }
        catch(Exception exception)
        {
            getErrorCodeFromResponse(httpurlconnection);
            return getMessage(host, exception);
        }
        try
        {
            setErrorCode(httpurlconnection.getResponseCode());
            String s4 = httpurlconnection.getContentType();
            if(s4 != null && encoding != null)
                s4 = rewriteEncoding(s4, encoding);
            if(s4 != null)
                httpservletresponse.setContentType(s4);
            else
                s4 = "";
            if(headerMap != null)
                rewriteHeaders(httpurlconnection, httpservletresponse);
            try
            {
            	obj1 = createInputStream(httpurlconnection);
                bufferedoutputstream = new BufferedOutputStream(httpservletresponse.getOutputStream());
                int i;
                while((i = ((InputStream) (obj1)).read()) >= 0) 
                    bufferedoutputstream.write(i);
            }
            catch(Exception exception2)
            {
                return getMessage(host, exception2);
            }
        }
        catch(Exception exception1)
        {
            return getMessage(host, exception1);
        }
        if(obj1 != null)
            ((InputStream) (obj1)).close();
        if(bufferedoutputstream != null)
        {
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        return null;
    }


    private String doDeleteSSL(String host, Hashtable<String,String> headerMap, String encoding, HttpServletResponse httpservletresponse) throws IOException
    {
        Object obj = null;
        HttpsURLConnection httpsurlconnection = null;
        Object obj1 = null;
        BufferedOutputStream bufferedoutputstream = null;
        String s2 = "";
        try
        {
            URL url = new URL(host);
            httpsurlconnection = (HttpsURLConnection)url.openConnection();
            httpsurlconnection.setRequestMethod("DELETE");
            httpsurlconnection.setDoInput(true);
            httpsurlconnection.setUseCaches(false);
            if(headerMap != null)
            {
                String s3;
                for(Enumeration enumeration = headerMap.keys(); enumeration.hasMoreElements(); httpsurlconnection.setRequestProperty(s3, (String)headerMap.get(s3)))
                    s3 = (String)enumeration.nextElement();

            }
        }
        catch(Exception exception)
        {
            getErrorCodeFromResponse(httpsurlconnection);
            return getMessage(host, exception);
        }
        try
        {
            setErrorCode(httpsurlconnection.getResponseCode());
            String s4 = httpsurlconnection.getContentType();
            if(s4 != null && encoding != null)
                s4 = rewriteEncoding(s4, encoding);
            if(s4 != null)
                httpservletresponse.setContentType(s4);
            else
                s4 = "";
            if(headerMap != null)
                rewriteHeaders(httpsurlconnection, httpservletresponse);
            try
            {
            	obj1 = createInputStream(httpsurlconnection);
                bufferedoutputstream = new BufferedOutputStream(httpservletresponse.getOutputStream());
                int i;
                while((i = ((InputStream) (obj1)).read()) >= 0) 
                    bufferedoutputstream.write(i);
            }
            catch(Exception exception2)
            {
                return getMessage(host, exception2);
            }
        }
        catch(Exception exception1)
        {
            return getMessage(host, exception1);
        }
        if(obj1 != null)
            ((InputStream) (obj1)).close();
        if(bufferedoutputstream != null)
        {
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        return null;
    }

    
    private String doHead(String host, Hashtable<String,String> headerMap, String encoding, HttpServletResponse httpservletresponse) throws IOException
    {
        Object obj = null;
        HttpURLConnection httpurlconnection = null;
        Object obj1 = null;
        BufferedOutputStream bufferedoutputstream = null;
        String s2 = "";
        try
        {
            URL url = new URL(host);
            httpurlconnection = (HttpURLConnection)url.openConnection();
            httpurlconnection.setRequestMethod("HEAD");
            httpurlconnection.setDoInput(true);
            httpurlconnection.setUseCaches(false);
            if(headerMap != null)
            {
                String s3;
                for(Enumeration<String> enumeration = headerMap.keys(); enumeration.hasMoreElements(); httpurlconnection.setRequestProperty(s3, headerMap.get(s3)))
                    s3 = enumeration.nextElement();

            }
        }
        catch(Exception exception)
        {
            getErrorCodeFromResponse(httpurlconnection);
            return getMessage(host, exception);
        }
        try
        {
            setErrorCode(httpurlconnection.getResponseCode());
            String s4 = httpurlconnection.getContentType();
            if(s4 != null && encoding != null)
                s4 = rewriteEncoding(s4, encoding);
            if(s4 != null)
                httpservletresponse.setContentType(s4);
            else
                s4 = "";
            if(headerMap != null)
                rewriteHeaders(httpurlconnection, httpservletresponse);
            try
            {
            	obj1 = createInputStream(httpurlconnection);
                bufferedoutputstream = new BufferedOutputStream(httpservletresponse.getOutputStream());
                int i;
                while((i = ((InputStream) (obj1)).read()) >= 0) 
                    bufferedoutputstream.write(i);
            }
            catch(Exception exception2)
            {
                return getMessage(host, exception2);
            }
        }
        catch(Exception exception1)
        {
            return getMessage(host, exception1);
        }
        if(obj1 != null)
            ((InputStream) (obj1)).close();
        if(bufferedoutputstream != null)
        {
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        return null;
    }

    
    private String doHeadSSL(String host, Hashtable<String,String> headerMap, String encoding, HttpServletResponse httpservletresponse) throws IOException
    {
        Object obj = null;
        HttpsURLConnection httpsurlconnection = null;
        Object obj1 = null;
        BufferedOutputStream bufferedoutputstream = null;
        String s2 = "";
        try
        {
            URL url = new URL(host);
            httpsurlconnection = (HttpsURLConnection)url.openConnection();
            httpsurlconnection.setRequestMethod("HEAD");
            httpsurlconnection.setDoInput(true);
            httpsurlconnection.setUseCaches(false);
            if(headerMap != null)
            {
                String s3;
                for(Enumeration<String> enumeration = headerMap.keys(); enumeration.hasMoreElements(); httpsurlconnection.setRequestProperty(s3, headerMap.get(s3)))
                    s3 = enumeration.nextElement();

            }
        }
        catch(Exception exception)
        {
            getErrorCodeFromResponse(httpsurlconnection);
            return getMessage(host, exception);
        }
        try
        {
            setErrorCode(httpsurlconnection.getResponseCode());
            String s4 = httpsurlconnection.getContentType();
            if(s4 != null && encoding != null)
                s4 = rewriteEncoding(s4, encoding);
            if(s4 != null)
                httpservletresponse.setContentType(s4);
            else
                s4 = "";
            if(headerMap != null)
                rewriteHeaders(httpsurlconnection, httpservletresponse);
            try
            {
            	obj1 = createInputStream(httpsurlconnection);
                bufferedoutputstream = new BufferedOutputStream(httpservletresponse.getOutputStream());
                int i;
                while((i = ((InputStream) (obj1)).read()) >= 0) 
                    bufferedoutputstream.write(i);
            }
            catch(Exception exception2)
            {
                return getMessage(host, exception2);
            }
        }
        catch(Exception exception1)
        {
            return getMessage(host, exception1);
        }
        if(obj1 != null)
            ((InputStream) (obj1)).close();
        if(bufferedoutputstream != null)
        {
            bufferedoutputstream.flush();
            bufferedoutputstream.close();
        }
        return null;
    }

    protected String getMessage(String s, Exception exception)
    {
        String s1 = exception.getClass().getName();
        int i = s1.lastIndexOf('.');
        s1 = s1.substring(i + 1);
        StringWriter stringwriter = new StringWriter();
        PrintWriter printwriter = new PrintWriter(stringwriter);
        exception.printStackTrace(printwriter);
        printwriter.close();
        return (new StringBuilder()).append("Request: ").append(s).append("\nException: ").append(s1).append(": ").append(exception.getMessage()).append("\n").append(stringwriter.getBuffer().toString()).toString();
    }

    private InputStreamReader getInputStreamReader(InputStream inputstream, String s) throws UnsupportedEncodingException
    {
        if(s == null)
            return new InputStreamReader(inputstream);
        else
            return new InputStreamReader(inputstream, s);
    }

    private String rewriteEncoding(String s, String s1)
    {
        if(s.indexOf("charset") < 0)
            return (new StringBuilder()).append(s).append(";charset=").append(s1).toString();
        int i = s.indexOf(";");
        if(i < 0)
            i = s.indexOf("charset");
        return (new StringBuilder()).append(s.substring(0, i)).append(";charset=").append(s1).toString();
    }

    private String getParamsFromHash(Hashtable hashtable)
    {
        String s = "";
        String s1 = "";
        for(Enumeration enumeration = hashtable.keys(); enumeration.hasMoreElements();)
        {
            if(s.length() > 0)
                s = (new StringBuilder()).append(s).append("&").toString();
            String s2 = (String)enumeration.nextElement();
            String s3 = s2;
            int i = s3.indexOf("<");
            if(i > 0)
                s3 = s3.substring(0, i);
            s = (new StringBuilder()).append(s).append(s3).append("=").toString();
            s = (new StringBuilder()).append(s).append(URLEncoder.encode((String)hashtable.get(s2))).toString();
        }

        return s;
    }

    private void rewriteHeaders(URLConnection urlconnection, HttpServletResponse httpservletresponse)
    {
        Map map = urlconnection.getHeaderFields();
        if(map != null)
        {
            Set set = map.keySet();
            Iterator iterator = set.iterator();
            do
            {
                if(!iterator.hasNext())
                    break;
                String s = (String)iterator.next();
                if(s != null && !s.equalsIgnoreCase("Content-Type"))
                {
                    List list = (List)map.get(s);
                    String s1 = "";
                    for(Iterator iterator1 = list.iterator(); iterator1.hasNext();)
                    {
                        if(s1.length() > 0)
                            s1 = (new StringBuilder()).append(s1).append(",").toString();
                        s1 = (new StringBuilder()).append(s1).append((String)iterator1.next()).toString();
                    }

                    httpservletresponse.setHeader(s, s1);
                }
            } while(true);
        }
    }

    private void rewriteSSLHeaders(HttpsURLConnection httpsurlconnection, HttpServletResponse httpservletresponse)
    {
        Map map = httpsurlconnection.getHeaderFields();
        if(map != null)
        {
            Set set = map.keySet();
            Iterator iterator = set.iterator();
            do
            {
                if(!iterator.hasNext())
                    break;
                String s = (String)iterator.next();
                if(s != null && !s.equalsIgnoreCase("Content-Type"))
                {
                    List list = (List)map.get(s);
                    String s1 = "";
                    for(Iterator iterator1 = list.iterator(); iterator1.hasNext();)
                    {
                        if(s1.length() > 0)
                            s1 = (new StringBuilder()).append(s1).append(",").toString();
                        s1 = (new StringBuilder()).append(s1).append((String)iterator1.next()).toString();
                    }

                    httpservletresponse.setHeader(s, s1);
                }
            } while(true);
        }
    }

    private void getErrorCodeFromResponse(URLConnection urlconnection)
    {
        try
        {
            if(urlconnection instanceof HttpURLConnection)
                getErrorCodeFromResponse((HttpURLConnection)urlconnection);
        }
        catch(Exception exception)
        {
            setErrorCode(500);
        }
    }

    private void getErrorCodeFromResponse(HttpURLConnection httpurlconnection)
    {
        try
        {
            setErrorCode(httpurlconnection.getResponseCode());
        }
        catch(Exception exception)
        {
            setErrorCode(500);
        }
    }

}
